Ext.define('YX.core.Login', {
	  extend : 'Ext.container.Container',
	  width : '100%',
	  renderTo : Ext.getBody(),
	  layout : 'vbox',
	  txtOrg : null,
	  txtAccount : null,
	  txtPwd : null,
	  cmbLanguage : null,
	  btnLogin : null,
	  pelLogin : null,
	  storeLoginInfo : null,
	  initComponent : function() {
		  this.txtOrg = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.organization,
			    anchor : '98%',
			    itemId : 'txtOrg',
			    listeners : {
				    scope : this,
				    blur : this.onTxtOrgBlur
			    }
		    });

		  this.txtAccount = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.account,
			    anchor : '98%',
			    itemId : 'txtAccount',
			    listeners : {
				    scope : this,
				    specialkey : this.onTxtAccountSpecialKey
			    }
		    });

		  this.txtPwd = Ext.create('Ext.form.field.Text', {
			    fieldLabel : T.label.password,
			    anchor : '98%',
			    inputType : 'password',
			    itemId : 'txtPwd',
			    listeners : {
				    scope : this,
				    specialkey : this.onTxtPwdSpecialKey
			    }
		    });

		  this.cmbLanguage = Ext.create('Ext.form.field.ComboBox', {
			    fieldLabel : T.label.language,
			    anchor : '98%',
			    itemId : 'cmbLanguage',
			    displayField : 'text',
			    valueField : 'value',
			    editable : false,
			    value : 'zh_CN',
			    store : Ext.create('Ext.data.ArrayStore', {
				      fields : ['value', 'text'],
				      data : [['zh_CN', '中文'], ['en', 'English']]
			      }),
			    listeners : {
				    scope : this,
				    select : this.onCmbLanguageChange
			    }
		    });

		  this.btnLogin = Ext.create('Ext.button.Button', {
			    text : T.label.login,
			    scope : this,
			    handler : this.login
		    });

		  this.pelLogin = Ext.create('Ext.form.Panel', {
			    style : 'margin-top:250px;margin-right:50px;background-color:#FFFFFF;',
			    width : 300,
			    header : {
				    title : T.label.login,
				    iconCls : 'icon-key'
			    },
			    defaults : {
				    labelWidth : 80
			    },
			    items : [this.txtOrg, this.txtAccount, this.txtPwd, this.cmbLanguage],
			    buttons : [this.btnLogin]
		    });

		  this.items = [{
			  xtype : 'component',
			  // componentCls: 'logo',
			  html : '<image src="resources/' + appName
			    + '/images/logo.png" style="width:120px;height:120px;"/>',
			  height : 64,
			  width : 64
		  }, {
			  xtype : 'container',
			  //componentCls : 'login',
			  width : '100%',
			  height : 442,
			  layout : {
				  type : 'hbox',
				  pack : 'end'
			  },
			  items : [this.pelLogin]
		  }];

		  this.storeLoginInfo = Ext.create('Ext.data.Store', {
			    fields : ['language', 'orgCode'],
			    proxy : {
				    type : 'localstorage',
				    id : 'logininfo'
			    },
			    listeners : {
				    scope : this,
				    load : this.onStoreLoginInfoLoad
			    }
		    });

		  this.on('afterrender', this.renderSelf, this);
		  this.callParent();
	  },
	  renderSelf : function() {
		  this.txtOrg.focus(true);
		  this.txtAccount.setValue('zxw');
		  this.txtPwd.setValue('zxw123');
		  this.storeLoginInfo.load();
	  },
	  login : function() {
		  if (this.btnLogin.disabled) {
			  return;
		  }
		  this.btnLogin.disable();

		  YX.ZbusRpc.request({
			    scope : this,
			    module : 'SecUserLoginService',
			    params : {
				    language : this.cmbLanguage.getValue(),
				    orgCode : this.txtOrg.getValue(),
				    loginUserId : this.txtAccount.getValue(),
				    loginPwd : this.txtPwd.getValue()
			    },
			    callback : function() {
				    this.btnLogin.enable();
			    },
			    success : function(rsp) {
				    LoginInfo.LANGUAGE = this.cmbLanguage.getValue();
				    LoginInfo.ORGCODE = this.txtOrg.getValue();
				    LoginInfo.USERNAME = this.txtAccount.getValue();
				    LoginInfo.TOKEN = rsp.requestToken;

				    this.loadTranslate();

				    this.storeLoginInfo.removeAll();
				    this.storeLoginInfo.add({
					      language : LoginInfo.LANGUAGE,
					      orgCode : LoginInfo.ORGCODE
				      });
				    this.storeLoginInfo.save();
				    
				    Ext.getBody().removeCls("body");
				    Ext.destroy(this);

				    Ext.require('YX.core.Main', function() {
					      var main = Ext.create('YX.core.Main', {
						        renderTo : Ext.getBody()
					        });
					      Ext.ClassManager.set('main', main);
					      main.show();
				      });
			    }
		    });
	  },
	  onCmbLanguageChange : function(combo, records) {
		  var lang = records[0].get('value');
		  this.changeLanguage(lang);
	  },
	  changeLanguage : function(lang) {
		  var els = Ext.getHead().query('script[src^=js/locale/]');
		  Ext.each(els, function(el) {
			    el.remove();
		    });
		  Ext.Loader.loadScript('js/locale/ext-lang-' + lang + '.js');
		  Ext.Loader.loadScript({
			    url : 'js/locale/app-lang-' + lang + '.js',
			    onLoad : function() {
				    this.txtOrg.setFieldLabel(T.label.organization);
				    this.txtAccount.setFieldLabel(T.label.account);
				    this.txtPwd.setFieldLabel(T.label.password);
				    this.cmbLanguage.setFieldLabel(T.label.language);
				    this.pelLogin.setTitle(T.label.login);
			    },
			    scope : this
		    });
	  },
	  onTxtOrgBlur : function(field) {
		  field.setValue(field.getValue().toUpperCase());
	  },
	  onTxtAccountSpecialKey : function(field, e) {
		  if (e.getKey() == e.ENTER) {
			  if (Ext.isEmpty(this.txtPwd.getValue())) {
				  this.txtPwd.focus();
			  } else {
				  this.login();
			  }
		  }
	  },
	  onTxtPwdSpecialKey : function(field, e) {
		  if (e.getKey() == e.ENTER) {
			  this.login();
		  }
	  },
	  onStoreLoginInfoLoad : function(store, records) {
		  if (!Ext.isEmpty(records)) {
			  this.cmbLanguage.setValue(records[0].get('language'));
			  this.changeLanguage(records[0].get('language'));
			  this.txtOrg.setValue(records[0].get('orgCode'));
			  this.txtAccount.focus(true);
		  } else {
			  this.txtOrg.focus(true);
		  }
	  },
	  loadTranslate : function() {
		  YX.ZbusRpc.request({
			    scope : this,
			    module : 'BasViewTranslateMsgListService',
			    params : {
				    msgLocale : LoginInfo.LANGUAGE,
				    msgSide : 'C'
			    },
			    success : function(rsp) {
				    if (!Ext.isEmpty(rsp.msgList)) {
					    Ext.each(rsp.msgList, function(msg) {
						      var msgCode = msg.msgCode;
						      if (Ext.String.startsWith(msgCode, 'T.', true)) {
							      msgCode = msgCode.substr(2);
						      }

						      if (Ext.String.startsWith(msgCode, 'label.')) {
							      T.label[msgCode.substr(6)] = msg.msgDesc;
						      } else if (Ext.String.startsWith(msgCode, "msg.")) {
							      msgCode = msgCode.substr(4).toLowerCase().replace(/ /g, '_');
							      T.msg[msgCode] = msg.msgDesc;
						      }
					      });
				    }
			    }
		    });
	  }
  });
